package yl.hs.bmipfull.controllers.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.system.IMenuInfoService;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

/**
 * 作用: 系统菜单控制层  <br/>
 * 创建时间: 2021-05-11 08:39 <br/>
 *
 * @author 张凯
 */
@Api(value = "系统菜单Web接口", tags = "系统管理——系统菜单操作")
@RequestMapping(value = "/api/menu")
@RestController
public class MenuInfoController extends ControllerBase {


    @Autowired
    private IMenuInfoService menuInfoService;

    @ApiOperation(value = "查询系统菜单", notes = "查询系统菜单信息")
    @RequestMapping(value = "/item", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            var data = menuInfoService.queryList(pageQuery);
            return success("查询成功", Tl.toTreeList(data, null));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询系统菜单", notes = "根据系统菜单编号查询系统菜单")
    @RequestMapping(value = "/item/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@PathVariable String sn) {

        return success("查询成功", menuInfoService.findBySn(sn));
    }


    @ApiOperation(value = "保存系统菜单", notes = "查询/更新系统菜单信息")
    @RequestMapping(value = "/item", method = RequestMethod.PUT)
    public WebResult saveMenu(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = menuInfoService.saveMenu(submitModel);
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "删除系统菜单信息", notes = "根据系统菜单编号删除系统菜单信息")
    @RequestMapping(path = "/item", method = RequestMethod.DELETE)
    @ResponseBody
    public WebResult delete(@ApiParam(value = "系统菜单编号", name = "sn", allowMultiple = true, required = true) @RequestParam("sn") String[] sn) {
        if (menuInfoService.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }
}
